import os
from PIL import Image

def images_merger(image_1_path: str, image_2_path: str, file_name: int):
    """
    image_1_path: 图像文件 1 目录
    image_2_path: 图像文件 2 目录
    file_name: 输出文件名
    图像拼接
    """
    image_1 = Image.open(fp=image_1_path)
    image_2 = Image.open(fp=image_2_path)
    new_img = Image.new('RGB', (900, 1200), 'white')

    new_img.paste(im=image_1, box=(0, 0))
    new_img.paste(im=image_2, box=(0, 600))
    os.remove(image_1_path)
    os.remove(image_2_path)

    new_img.save(fp='./images/' + str(file_name) + '.jpeg')

def mearge_all_images()->None:
    files_name = os.listdir('./images')
    images_name = list()
    for file in files_name:
        if '.jpeg' in file:
            images_name.append(file)
    images_name.sort(key=lambda x: int(x.split('_')[0]))
    file_name = 1
    for i in range(0, len(images_name)-1, 2):
        images_merger(image_1_path='./images/'+images_name[i], image_2_path='./images/'+images_name[i+1], file_name=file_name)
        file_name += 1
    print('>>> Step.5 全部图形合并成功.')
mearge_all_images()